<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="800px" 
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="Model" :rules="dialogProps.action === 'add' ? addRules : editRules" 
      ref="from" label-width="80px" label-position="right" size="small">
        <el-row>
            <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                <el-input :maxlength="32" v-model="Model.name"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="档案号" prop="fileNumber">
                <el-input :maxlength="32" v-model="Model.fileNumber"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="体检时间" prop="reportTime">
                <el-date-picker ref="focus" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="Model.reportTime" style="width: 100%;"></el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="12">
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="数据文件" prop="report">
                <el-input :maxlength="32" v-model="Model.report"></el-input>
                <el-upload
                class="upload-demo"
                action="http://localhost:8080/report/fileUpload"
                :before-upload="beforeUpload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="1"
                :on-exceed="handleExceed"
                :file-list="fileList">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">--------------------</div>
                </el-upload>
                </el-form-item>
            </el-col>
            <el-col :span="12">
            </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('from')">确 定 上 传</el-button>
    </span>    
  </el-dialog>
</template>


<script>
import { saveUser, updateUser } from '@/api/user'
import {updateEmp} from '@/api/emp'
import { addReport,updateReport,fileUpload } from '@/api/report'

export default {
  name: "user-edit",
  data() {
    // var validatePassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请输入密码'));
    //   } else {
    //     if (this.Model.checkPassword !== '') {
    //       this.$refs.from.validateField('checkPassword');
    //     }
    //     callback()
    //   }
    // }
    // var validateCheckPassword = (rule, value, callback) => {
    //   if (value === '' || value === undefined) {
    //     callback(new Error('请再次输入密码'))
    //   } else if (value !== this.Model.password) {
    //     callback(new Error('两次输入密码不一致!'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loading: false,
      Model: {
        id:0,
        fileNumber:'',
        name:'',
        phoneNumber:'',
        reportTime:'',
        report:''
      },
      fileList: [],

      roleCheckboxList: {
        checkAll: false,
        isIndeterminate: true,
        roles: []
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      activeTabName: 'tab1',
      addRules: {
        
      },
      editRules: {
        
      }
    };
  },
  methods: {
    //文件上传方法
    handleRemove(file, fileList) {
    console.log(file, fileList);
    },
    handlePreview(file) {
    console.log(file);
    },
    handleExceed(files, fileList) {
    this.$message.warning('限制选择 1 个文件');
    },
    beforeRemove(file, fileList) {
    },
    beforeUpload(file){
    let fd = new FormData();
    fd.append('file',file);//传文件
    fileUpload(fd).then(response => {
      console.log(response.data)
      if(response.data=='failed'){
        return false
      }
      this.Model.report=response.data
    })
    return false  //屏蔽了action的默认上传
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveUser()
          } else {
            this.doUpdateUser()
          }
          
        } else {
          return false
        }
      })
      
    },
    doUpdateUser() {
      this.loading = true
      console.log(this.Model)
      updateReport(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)      
      })
      
    },
    doSaveUser() {
      this.loading = true
      console.log(this.Model)
      addReport(this.Model)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.clearFrom()
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error) 
      })
      
    },    
    clearFrom(){
      for(let key in this.Model){
        this.Model[key]  = ''
        if(key=='status'){
        //   this.Model[key]  = '启用'
        }
      }
    },
    handleDialogClose() {
      this.dialogProps.visible = false  
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['from'].clearValidate()
        this.$refs['focus'].focus()
      })
    },
    getAllRoleId() {
      let allRoleId = []
      for(let role of this.roleCheckboxList.roles) {
        allRoleId.push(role.id)
      }
      return allRoleId
    },
    handleCheckAllChange(val) {
      this.Model.roleIds = (val ? this.getAllRoleId() : [])
      this.roleCheckboxList.isIndeterminate = false
    },
    handleCheckedRolesChange(value) {
      let checkedCount = value.length
      this.roleCheckboxList.checkAll = checkedCount === this.roleCheckboxList.roles.length
      this.roleCheckboxList.isIndeterminate = checkedCount > 0 && checkedCount < this.roleCheckboxList.roles.length
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })      
    }
  },
  mounted: function() {
    this.$nextTick(() => {  
      this.$on('openEditUserDialog', function(model) {
        console.log(model)
        this.Model = model
        // this.roleCheckboxList.roles = roles        
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '上传报告'
        this.activeTabName = 'tab1'
        this.dialogProps.visible = true
      })
      this.$on('openAddUserDialog', function() {
        // this.Model = {
        //   roleIds: []
        // }
        // this.roleCheckboxList.roles = roles
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增报告'
        this.activeTabName = 'tab1'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style lang="scss" scoped>
  .user-info-tab-pane {
    margin-top: 10px;
  }
</style>





